package com.bridgeintelligent.tag.webserver.dim.mgmt.service.dimfree;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.catalog.model.DimFreeModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.catalog.model.DimListModel;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by JackCheung on 2019/9/12 2:06 PM.
 */
public interface IDimFreeService {

    DimRep add(DimFreeModel dimFreeModel, MultipartFile multipartFile);

    DimRep list(DimListModel dimListModel);

    DimRep detail(String dimId);

    DimRep del(String dimId);

    DimRep delList(DimListModel dimListModel);

    DimRep clear(String dimId);

    void updateApprovaleState(String dimId, String approvalState);

    void updateApproveState(String dimId, String approvalState, String approveDate);

    DimRep updateBaseInfo(DimFreeModel dimFreeModel);

    DimRep addCusts(String dimId, MultipartFile multipartFile, String fileDescription);

    DimRep delCusts(String dimId, MultipartFile multipartFile, String fileDescription);

    void downLoad(String id, HttpServletResponse response, HttpServletRequest req);

    default File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    default void inputStreamToFile(InputStream ins, File file) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os != null){
                try {
                    os.close();
                }catch (Exception e){

                }
            }
            if (ins != null){
                try {
                    ins.close();
                }catch (Exception e){

                }
            }
        }
    }

    DimRep addById(DimFreeModel dimFreeModel);

    DimRep up(String id, boolean ecifFlag, MultipartFile multipartFile);

    void downLoadError(String id, HttpServletResponse response, HttpServletRequest req);
    int doFileBakup(String type,String dimId,String startDate,String endDate);
}
